package com.alisls.demo.springcloud.service.im.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@RefreshScope
public class RabbitMqConfig {

    /**
     * 即时消息交换器
     */
    @Value("${im.exchange}")
    private String IM_FANOUT_EXCHANGE;

    /**
     * 即时消息队列
     */
    @Value("${im.queue}")
    private String IM_QUEUE;

    /**
     * 在RabbitMq中创建交换器
     * @return
     */
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange(IM_FANOUT_EXCHANGE, true, false);
    }

    /**
     * 在rabbitmq中创建队列
     * @return
     */
    @Bean
    public Queue imQueue() {
        return new Queue(IM_QUEUE, true);
    }

    /**
     * 将队列绑定到交换器
     * @param queue
     * @param fanoutExchange
     * @return
     */
    @Bean
    Binding fanoutBinding() {
        return BindingBuilder.bind(imQueue()).to(fanoutExchange());
    }

}
